﻿using System;
using System.Linq;

using SportTraining.Core.Dal;
using SportTraining.Core.Domain;

namespace SportTraining.Core.Linq2Sql.Dal
{
    public class PaymentDal : BaseDal, IPaymentDal
    {
        public PaymentDal(IDataContext dataContext) : base(dataContext)
        {
        }

        #region Implementation of IBaseDal<IPayment>

        public IPayment GetById(int id)
        {
            throw new NotImplementedException();
        }

        public void Add(IPayment entity)
        {
            if (!(entity is Payment))
            {
                throw new ArgumentException("Bad Payment class");
            }

            dataContext.Payments.InsertOnSubmit((Payment) entity);
        }

        public void Delete(IPayment entity)
        {
            throw new NotImplementedException();
        }

        public IQueryable<IPayment> SelectAll()
        {
            throw new NotImplementedException();
        }

        public IPayment CreatePayment(ISection section)
        {
            var sectionInDb = dataContext.Sections.FirstOrDefault(item => item.Id == section.Id);

            if (sectionInDb == null)
            {
                throw new ArgumentException(string.Format("Секция {0} не найдена в БД", section));
            }

            if (!(section is Section))
            {
                throw new ArgumentException("Bad Section class");
            }

            if (!(section.ActualPrice is Price))
            {
                throw new ArgumentException("Bad Price class");
            }

            return new Payment {Price = (Price) section.ActualPrice};
        }
        #endregion
    }
}
